<template>
  <div class="personnel-list-container">
    <!-- 顶部操作栏 -->
    <div class="top-bar">
      <div class="title-section">
        <h2>人员管理</h2>
        <el-button 
          type="primary" 
          icon="el-icon-plus" 
          @click="handleAdd"
          class="add-btn"
        >
          添加人员
        </el-button>
      </div>
      
      <!-- 搜索表单 -->
      <div class="search-section">
        <el-form :inline="true" :model="searchForm" class="search-form">
          <el-form-item label="姓名">
            <el-input v-model="searchForm.name" placeholder="请输入姓名" clearable></el-input>
          </el-form-item>
          <el-form-item label="职位">
            <el-input v-model="searchForm.position" placeholder="请输入职位" clearable></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-select
              v-model="searchForm.gender"
              placeholder="请选择性别"
              clearable
              style="width: 120px"
            >
              <el-option
                v-for="item in genderOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button-group>
              <el-button 
                type="primary" 
                icon="el-icon-search"
                @click="handleSearch"
                class="search-btn"
              >
                搜索
              </el-button>
              <el-button 
                icon="el-icon-refresh"
                @click="resetSearch"
                class="reset-btn"
              >
                重置
              </el-button>
            </el-button-group>
          </el-form-item>
        </el-form>
      </div>
    </div>

    <!-- 数据表格 -->
    <el-card class="table-card">
      <el-table
        :data="personnelList"
        v-loading="loading"
        border
        stripe
        style="width: 100%"
      >
        <el-table-column prop="name" label="姓名" min-width="100">
          <template #default="{ row }">
            <el-tag size="small">{{ row.name }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="age" label="年龄" width="80" align="center" />
        <el-table-column prop="gender" label="性别" width="80" align="center">
          <template #default="{ row }">
            <el-tag :type="row.gender === '男' ? 'primary' : 'success'" size="small">
              {{ row.gender }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="position" label="职位" min-width="120">
          <template #default="{ row }">
            <el-tag type="info" size="small">{{ row.position }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="联系方式" min-width="120" />
        <el-table-column prop="email" label="电子邮件" min-width="180" />
        <el-table-column label="操作" width="200" fixed="right" align="center">
          <template #default="{ row }">
            <el-button-group>
              <el-button
                type="primary"
                size="small"
                icon="el-icon-edit"
                @click="handleEdit(row)"
              >
                编辑
              </el-button>
              <el-button
                type="danger"
                size="small"
                icon="el-icon-delete"
                @click="handleDelete(row)"
              >
                删除
              </el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :page-size="pageSize"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 添加/编辑对话框 -->
    <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      width="60%"
      :close-on-click-modal="false"
      :destroy-on-close="true"
      class="personnel-dialog"
    >
      <div class="dialog-content">
        <div class="dialog-header">
          <div class="header-icon">
            <i :class="isEdit ? 'el-icon-edit' : 'el-icon-plus'"></i>
          </div>
          <p class="header-desc">{{ isEdit ? '修改人员的基本信息' : '添加新的人员信息' }}</p>
        </div>

        <el-form
          ref="formRef"
          :model="formData"
          :rules="rules"
          label-width="100px"
          @submit.prevent="handleSubmit"
          class="personnel-form"
        >
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="姓名" prop="name">
                <el-input 
                  v-model="formData.name" 
                  placeholder="请输入姓名"
                  clearable
                  :prefix-icon="'el-icon-user'"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="年龄" prop="age">
                <el-input-number 
                  v-model="formData.age" 
                  :min="18" 
                  :max="100"
                  style="width: 100%"
                  controls-position="right"
                />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="性别" prop="gender">
                <el-radio-group v-model="formData.gender" class="gender-group">
                  <el-radio label="男">男</el-radio>
                  <el-radio label="女">女</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="职位" prop="position">
                <el-input 
                  v-model="formData.position" 
                  placeholder="请输入职位"
                  clearable
                  :prefix-icon="'el-icon-office-building'"
                />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="联系方式" prop="phone">
                <el-input 
                  v-model="formData.phone" 
                  placeholder="请输入联系方式"
                  clearable
                  :prefix-icon="'el-icon-phone'"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="电子邮件" prop="email">
                <el-input 
                  v-model="formData.email" 
                  placeholder="请输入电子邮件"
                  clearable
                  :prefix-icon="'el-icon-message'"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false" plain>取消</el-button>
          <el-button 
            type="primary" 
            @click="handleSubmit" 
            :loading="loading"
            class="submit-btn"
          >
            {{ isEdit ? '保存修改' : '确认添加' }}
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, onMounted, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import PersonnelService from '@/services/personnel.service'

export default {
  name: 'PersonnelList',
  setup() {
    const router = useRouter()
    const loading = ref(false)
    const personnelList = ref([])
    const total = ref(0)
    const pageSize = ref(10)
    const currentPage = ref(1)
    
    const searchForm = ref({
      name: '',
      position: '',
      gender: ''
    })

    const genderOptions = [
      { label: '男', value: '男' },
      { label: '女', value: '女' }
    ]

    const dialogVisible = ref(false)
    const dialogTitle = ref('添加人员')
    const isEdit = ref(false)
    const formRef = ref(null)
    const formData = reactive({
      name: '',
      age: 18,
      gender: '',
      position: '',
      phone: '',
      email: ''
    })

    const rules = {
      name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
      age: [{ required: true, message: '请输入年龄', trigger: 'blur' }],
      gender: [{ required: true, message: '请选择性别', trigger: 'change' }],
      position: [{ required: true, message: '请输入职位', trigger: 'blur' }],
      phone: [{ required: true, message: '请输入联系方式', trigger: 'blur' }],
      email: [
        { required: true, message: '请输入邮箱地址', trigger: 'blur' },
        { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
      ]
    }

    const loadPersonnel = async () => {
      try {
        loading.value = true;
        const params = {
          name: searchForm.value.name?.trim() || '',
          position: searchForm.value.position?.trim() || '',
          gender: searchForm.value.gender || '',
          page: currentPage.value - 1,
          size: pageSize.value
        };
        
        const response = await PersonnelService.getAll(params);
        if (response.content) {
          personnelList.value = response.content;
          total.value = response.totalElements;
        } else {
          personnelList.value = [];
          total.value = 0;
        }
      } catch (error) {
        ElMessage.error('获取人员列表失败');
        console.error('Error:', error);
      } finally {
        loading.value = false;
      }
    };

    const handleSearch = () => {
      currentPage.value = 1
      loadPersonnel()
    }

    const resetSearch = () => {
      searchForm.value = {
        name: '',
        position: '',
        gender: ''
      }
      handleSearch()
    }

    const handleSizeChange = (val) => {
      pageSize.value = val
      loadPersonnel()
    }

    const handleCurrentChange = (val) => {
      currentPage.value = val
      loadPersonnel()
    }

    const handleEdit = (row) => {
      isEdit.value = true
      dialogTitle.value = '编辑人员信息'
      Object.assign(formData, row)
      dialogVisible.value = true
    }

    const handleDelete = async (row) => {
      try {
        await ElMessageBox.confirm(
          `确定要删除 ${row.name} 的信息吗？`,
          '警告',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        )
        
        await PersonnelService.delete(row.id)
        ElMessage.success('删除成功')
        loadPersonnel()
      } catch (error) {
        if (error !== 'cancel') {
          ElMessage.error('删除失败')
        }
      }
    }

    const resetForm = () => {
      formData.name = ''
      formData.age = 18
      formData.gender = ''
      formData.position = ''
      formData.phone = ''
      formData.email = ''
      if (formRef.value) {
        formRef.value.resetFields()
      }
    }

    const handleAdd = () => {
      isEdit.value = false
      dialogTitle.value = '添加人员'
      resetForm()
      dialogVisible.value = true
    }

    const handleSubmit = async () => {
      if (!formRef.value) return
      
      try {
        await formRef.value.validate()
        loading.value = true

        if (isEdit.value) {
          await PersonnelService.update(formData.id, formData)
          ElMessage.success('更新成功')
        } else {
          await PersonnelService.create(formData)
          ElMessage.success('添加成功')
        }

        dialogVisible.value = false
        loadPersonnel()
      } catch (error) {
        ElMessage.error(isEdit.value ? '更新失败' : '添加失败')
      } finally {
        loading.value = false
      }
    }

    onMounted(() => {
      loadPersonnel()
    })

    return {
      loading,
      personnelList,
      searchForm,
      total,
      pageSize,
      currentPage,
      handleSearch,
      resetSearch,
      handleSizeChange,
      handleCurrentChange,
      handleEdit,
      handleDelete,
      genderOptions,
      dialogVisible,
      dialogTitle,
      isEdit,
      formRef,
      formData,
      rules,
      handleAdd,
      handleSubmit
    }
  }
}
</script>

<style scoped>
.personnel-list-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 60px);
  animation: fadeIn 0.5s ease-out;
}

.top-bar {
  margin-bottom: 20px;
}

.title-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.title-section h2 {
  margin: 0;
  color: #303133;
  font-size: 24px;
}

.search-section {
  background: linear-gradient(135deg, #ffffff 0%, #f8f9fb 100%);
  border: 1px solid #ebeef5;
  padding: 20px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  transition: all 0.3s;
}

.search-section:hover {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
}

.search-form {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.table-card {
  margin-top: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 20px;
  padding: 10px 0;
}

:deep(.el-tag) {
  margin: 0 2px;
}

:deep(.el-button-group) {
  display: flex;
  gap: 5px;
}

:deep(.el-table) {
  margin-top: 10px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

:deep(.el-table th) {
  background: linear-gradient(90deg, #f5f7fa 0%, #f8f9fb 100%) !important;
  font-weight: 600;
}

:deep(.el-table td) {
  padding: 12px 0;
}

:deep(.el-table .hover-row td) {
  background-color: #f5f7fa;
  transition: background-color 0.3s;
}

:deep(.el-table th) {
  background-color: #f5f7fa !important;
}

:deep(.el-form-item) {
  margin-bottom: 0;
}

.dialog-footer {
  text-align: right;
  padding-top: 20px;
}

:deep(.el-dialog__body) {
  padding: 20px 40px;
}

:deep(.el-input-number) {
  width: 100%;
}

:deep(.el-select) {
  width: 100%;
}

/* 对话框相关样式 */
:deep(.personnel-dialog .el-dialog) {
  border-radius: 8px;
  overflow: hidden;
  transform: translateY(-20px);
  opacity: 0;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

:deep(.personnel-dialog.el-dialog--center) {
  transform: translateY(0);
  opacity: 1;
}

:deep(.personnel-dialog .el-dialog__header) {
  margin: 0;
  padding: 20px 20px 0;
  text-align: center;
}

:deep(.personnel-dialog .el-dialog__title) {
  font-size: 20px;
  font-weight: 500;
  color: #303133;
}

.dialog-content {
  padding: 0 20px;
  background: linear-gradient(135deg, #ffffff 0%, #f8f9fb 100%);
  border-radius: 8px;
  margin: 0 20px;
}

.dialog-header {
  text-align: center;
  margin-bottom: 30px;
}

.header-icon {
  width: 60px;
  height: 60px;
  line-height: 60px;
  margin: 0 auto 15px;
  border-radius: 50%;
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
  color: white;
  font-size: 28px;
}

.header-desc {
  color: #909399;
  font-size: 14px;
  margin: 0;
}

.personnel-form {
  padding: 20px 0;
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: #606266;
}

:deep(.el-input__inner) {
  height: 40px;
  line-height: 40px;
  border-radius: 4px;
  transition: all 0.3s;
}

:deep(.el-input__inner:hover) {
  border-color: #1890ff;
}

:deep(.el-input__prefix) {
  left: 10px;
  color: #909399;
}

:deep(.el-input__inner:focus) {
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
}

.gender-group {
  width: 100%;
  display: flex;
  gap: 30px;
}

.dialog-footer {
  text-align: right;
  padding: 20px 20px;
  border-top: 1px solid #ebeef5;
  margin-top: 10px;
}

.submit-btn {
  padding: 12px 25px;
  font-size: 14px;
  border-radius: 4px;
  transition: all 0.3s;
}

.submit-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
}

/* 响应式调整 */
@media screen and (max-width: 768px) {
  :deep(.personnel-dialog .el-dialog) {
    width: 90% !important;
    margin: 20px auto;
  }
  
  .el-row {
    margin: 0 !important;
  }
  
  .el-col {
    padding: 0 !important;
  }
  
  .personnel-form {
    padding: 10px 0;
  }
}

/* 添加新样式 */
.add-btn {
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
  border: none;
  padding: 12px 20px;
  transition: all 0.3s;
}

.add-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.4);
}

.search-btn, .reset-btn {
  padding: 9px 15px;
  transition: all 0.3s;
}

.search-btn {
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
  border: none;
}

.search-btn:hover, .reset-btn:hover {
  transform: translateY(-1px);
}

/* 标签样式优化 */
:deep(.el-tag) {
  border-radius: 4px;
  padding: 0 10px;
  height: 24px;
  line-height: 24px;
  transition: all 0.3s;
}

:deep(.el-tag:hover) {
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* 表格行hover效果 */
:deep(.el-table__row) {
  transition: all 0.3s;
}

:deep(.el-table__row:hover) {
  background-color: #f5f7fa !important;
  transform: translateY(-2px);
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

/* 响应式优化 */
@media screen and (max-width: 768px) {
  .search-form {
    flex-direction: column;
  }
  
  .el-form-item {
    margin-right: 0;
    width: 100%;
  }
  
  .el-button-group {
    width: 100%;
    display: flex;
  }
  
  .el-button-group .el-button {
    flex: 1;
  }
}
</style> 